package com.jp;

/**
 * 特定业务对 IntervalSelector 的管理
 */
public class SpecificBusiness {
    private IntervalSelector.My my;
    private IntervalSelector view;
    private final GenericCallback<Boolean, Void, Void, Void> selectedListener;
    private boolean selected, selecting;

    public SpecificBusiness(IntervalSelector view, GenericCallback<Boolean, Void, Void, Void> selectedListener) {
        this.my = view.my;
        this.view = view;
        this.selectedListener = selectedListener;
        this.view.setSelectedListener(selectedListener);
    }

    public String assembleResultText() {
        if (my.applyLValue == null || my.applyRValue == null) return "";
        return my.unit + my.applyLValue + " - " + my.unit + my.applyRValue;
    }

    /**
     * apply 的时候调用
     */
    public void apply() {
        my.applyLValue = my.curLValue;
        my.applyRValue = my.curRValue;

    }

    public int getApplyLValue() {
        return my.applyLValue == null ? 0 : my.applyLValue;
    }

    public int getApplyRValue() {
        return my.applyRValue == null ? 0 : my.applyRValue;
    }

    /**
     * dialog dismiss 时调用
     */
    public void restore() {
        if (my.applyLValue != null && my.applyRValue != null) {
            my.curLValue = my.applyLValue;
            my.curRValue = my.applyRValue;
        } else if (my.L != null && my.R != null) {
            my.curLValue = my.L;
            my.curRValue = my.R;
        }
        my.move2ValueCoordinate(view);
        view.invalidate();
        selectedListener.onCall(selected(), null, null);

    }


    public void reset(boolean resetApplyPrice) {
        my.reset(resetApplyPrice);
        view.invalidate();
        selectedListener.onCall(false, null, null);
    }

    public void initLR(final int L, final int R) {
        if (my.L != null && my.R != null) return;
        if (my.L == null || my.R == null) {
            my.L = L;
            my.R = R;
        }
        if (my.curLValue == Integer.MIN_VALUE || my.curRValue == Integer.MIN_VALUE) {
            my.curLValue = L;
            my.curRValue = R;
        }
        my.initStep(view);

        my.move2ValueCoordinate(view);
        view.invalidate();
    }

    public boolean selected() {
        return my.L != null && my.R != null && (my.curLValue != my.L || my.curRValue != my.R);
    }

}
